package com.sqx.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Query;
import com.sqx.modules.sys.dao.SysDictDao;
import com.sqx.modules.sys.entity.SysDictEntity;
import com.sqx.modules.sys.service.SysDictService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("sysDictService")
public class SysDictServiceImpl extends ServiceImpl<SysDictDao, SysDictEntity> implements SysDictService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = (String) params.get("name");
        String parentId = (String) params.get("parentId");

        IPage<SysDictEntity> page = this.page(
                new Query<SysDictEntity>().getPage(params),
                new QueryWrapper<SysDictEntity>()
                        .like(StringUtils.isNotBlank(name), "name", name)
                        .eq(StringUtils.isNotBlank(parentId), "parent_id", parentId)
        );

        return new PageUtils(page);
    }

    @Override
    public List<SysDictEntity> getDictList(String name) {
        SysDictEntity dict = baseMapper.selectOne(new QueryWrapper<SysDictEntity>().eq("name", name));
        if (dict == null) {
            return null;
        }
        return baseMapper.selectList(new QueryWrapper<SysDictEntity>().eq("parent_id", dict.getId()).orderByAsc("order_num"));
    }

}
